package com.ex.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ex.model.User;
import com.ex.service.UserService;

@Controller
@RequestMapping("/user/")
public class UserController {
	@Autowired
	UserService service;
	
	@RequestMapping("show")
	public String show(){
		System.out.println("进入到用户的控制层了");
		return "/index.jsp";
	}
	
	@RequestMapping("findUser")
	public String findUser(@RequestParam(value="username",required=false)String username,
			@RequestParam(value="password")String password,HttpServletRequest request){
		System.out.println("进入到findUser");
		System.out.println("username:"+username);
		System.out.println("password:"+password);
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		request.setAttribute("id", 1);
		return "/findUser.jsp";
	}
	
	@RequestMapping("edit")
	public void findEdit(@RequestParam(value="id",required=false)String id,
			@RequestParam(value="name",required=false)String name,@RequestParam(value="address",required=false)String address,HttpServletRequest request,HttpServletResponse response) throws IOException{
		System.out.println("进入到edit");
		System.out.println("id:"+id);
		System.out.println("name:"+name);
		System.out.println("username:"+name);
		//response.sendRedirect("/springmvcdemo/user/findUser.jsp");
		String jsonstr="{\"succ\":1}";
		response.getWriter().print(jsonstr);
	}
	
	@RequestMapping(value="/detail/{a}")
	@ResponseBody
	public Map<String, Object> detail(@PathVariable(value="a")Integer id) throws IOException{
		
		System.out.println("id:"+id);
		List<User> list = service.queryUser(id);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = null;
		if(list.size() == 1){
			user = list.get(0);
			map.put("user", user);
		} else {
			map.put("user", 1);
		}
		return map;	
	}
	
	
}
